<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
class HomeController extends Controller
{
    public function index()
    {
        $user=DB::table('user')->where('id',session('uid'))->first();
        // dd($user);
        session(['user'=>$user]);
        //网站配置信息
        $config=DB::table('config')->first();
        // dd($config);
        //网站友情链接
        $link=DB::table('link')->get();
        // dd($link);

        $goodsad=DB::table('goodsad')->first();

        //网站轮播图片信息
        $lunbo=DB::table('lunbo')->get();
        // dd($lunbo);

        //网站配置信息
        $someabout=DB::table('someabout')->first();
    	return view('extend.home',['config'=>$config,'lunbo'=>$lunbo,'link'=>$link,'goodsad'=>$goodsad,'someabout'=>$someabout]);
    }

    //404显示
    public function error()
    {
    	return view('home.404');
    }

    
    //
    public function blog_single()
    {
    	return view('home.blog_single');
    }

    //前台联系方式
    public function contact()
    {
    	return view('home.contact');
    }

    //前台登录
    public function login()
    { 
        $dat= $this->share();
    	return view('home.login',['config'=>$dat['config'],'lunbo'=>$dat['lunbo'],'link'=>$dat['link'],'goodsad'=>$dat['goodsad'],'someabout'=>$dat['someabout']]);
    }

    //前台登录及退出登录
    public function dologin(Request $request)
    {
        // $this->validate($request,[
        //     'username'=>'required|regex:/^\w{3-16}$/',
        //     ],[
        //     'username.required'=>'用户名不能为空',
        //     'username.regex'=>'用户名格式不正确',
        //     ]);
        $data=$request->except(['_token']);
        $user=DB::table('user')->where('username',$data['username'])->first();
       
        if(!$user){
            return back()->with('error','用户名不存在');
        }

        $res=Hash::check($data['password'],$user->password);

        if($res){
           session(['hid'=>$user->id,'username'=>$user->username]);
            return redirect('/')->with('info','登录成功');
        }else{
            return back()->with('error','密码错误');
        }    
    }
    //用户退出登录
    public function logout(Request $request)
    {
        $request->session()->forget('hid');
        $request->session()->forget('username');
        return redirect('/');
    }


    //前台用户注册
    public function register()
    {
        // dd(44);
        $dat= $this->share();
        // dd($data);
        return view('home.register',['config'=>$dat['config'],'lunbo'=>$dat['lunbo'],'link'=>$dat['link'],'goodsad'=>$dat['goodsad'],'someabout'=>$dat['someabout']]);

    }

    public function share()
    {
        $dat=[];
        $dat['user']=DB::table('user')->where('id',session('uid'))->first();
        // dd(session('uid'));
        session(['user'=>$dat['user']]);
        //网站配置信息
        $dat['config']=DB::table('config')->first();
        // dd($config);
        //网站友情链接
        $dat['link']=DB::table('link')->get();
        // dd($link);

        $dat['goodsad']=DB::table('goodsad')->first();

        //网站轮播图片信息
        $dat['lunbo']=DB::table('lunbo')->get();
        // dd($lunbo);

        //网站配置信息
        $dat['someabout']=DB::table('someabout')->first();

        return $dat;

    }
    //前台post
    public function regp()
    {
        return view('home.post');
    }

    //前台商品列表
    public function goods_list()
    {
    	return view('home.goods_list');
    }

    //前台商品详情
    public function goods_info()
    {
        $dat= $this->share();
        // dd($dat);
        return view('home.goods_info',['config'=>$dat['config'],'user'=>$dat['user'],'lunbo'=>$dat['lunbo'],'link'=>$dat['link'],'goodsad'=>$dat['goodsad'],'someabout'=>$dat['someabout']]);
       
    	// return view('home.goods_info');
    }

    //前台购物车
    public function wishlist()
    {
    	return view('home.wishlist');
    }


    //用户个人信息
    public function detail()
    {
         $dat= $this->share();
        // dd($dat);
        return view('home.detail',['config'=>$dat['config'],'user'=>$dat['user'],'lunbo'=>$dat['lunbo'],'link'=>$dat['link'],'goodsad'=>$dat['goodsad'],'someabout'=>$dat['someabout']]);
        // return view('home.detail');
    }

    public  function getBlog(){
        // dd(44);
        $dat= $this->share();
        return view('home.blog',['config'=>$dat['config'],'lunbo'=>$dat['lunbo'],'link'=>$dat['link'],'goodsad'=>$dat['goodsad'],'someabout'=>$dat['someabout']]);

    }

   

}
